package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmDataTypeFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmDataTypeRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmDataType;
import com.harmontronics.hanma.paas.paas.utils.MyBeanUtils;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmDataTypeMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmDataTypePO;
import java.util.List;

@Service("hmDataTypeService")
@DS("#header.App-Unique")
public class HmDataTypeService{

    @Autowired
    private HmDataTypeRepository hmDataTypeRepository;

    @Autowired
    private HmDataTypeMapper hmDataTypeMapper;


    public HmDataType findById( Integer id){
            HmDataTypePO po =   hmDataTypeRepository.getById(id);
        return BeanUtil.toBean(po,HmDataType.class);
    }

    public void save(HmDataType hmDataType){
            HmDataTypePO po =  BeanUtil.toBean(hmDataType,HmDataTypePO.class);
            hmDataTypeRepository.save(po);
    }

    public void update(HmDataType hmDataType){
            HmDataTypePO po =  BeanUtil.toBean(hmDataType,HmDataTypePO.class);
            hmDataTypeRepository.updateById(po);
    }

    public void remove(List<Long> ids){
            hmDataTypeRepository.removeByIds(ids);
    }

    public List<HmDataType> getList(Long appId){
        QueryWrapper<HmDataTypePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmDataTypeFields.APP_ID, appId);
        queryWrapper.orderByAsc(HmDataTypeFields.ID);
        List<HmDataType> typeList = new ArrayList<>();
        List<HmDataTypePO> poList = hmDataTypeRepository.list(queryWrapper);
        typeList = MyBeanUtils.batchTransform(HmDataType.class,poList);
        return typeList;
    }

    public void createBatch(List<HmDataType> dataTypeDtoList) {
        for(HmDataType dataTypeDto:dataTypeDtoList){
            QueryWrapper<HmDataTypePO> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq(HmDataTypeFields.APP_ID,dataTypeDto.getAppId());
            queryWrapper.eq(HmDataTypeFields.CODE,dataTypeDto.getCode());
            HmDataTypePO dbDataType = hmDataTypeMapper.selectOne(queryWrapper);
            if(dbDataType!=null){
                continue;
            }
            save(dataTypeDto);
        }
    }

    public HmDataTypePO findByCode(Long appId, String code) {
        QueryWrapper<HmDataTypePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmDataTypeFields.APP_ID, appId);
        queryWrapper.eq(HmDataTypeFields.CODE, code);
        return hmDataTypeMapper.selectOne(queryWrapper);
    }

}